var SharedData = {};
var POSItemStructure = {fav:[],cats:[]};
function loadSharedData()
{
	var services = [];
	services.push(["/popupService",{doClass : "loadGeneral"},function(data,args) { SharedData.General = JSON.parse(data); },null]);
	services.push(["/POSTableService",{func : "getAllTable"},function(data,args) { SharedData.Table = JSON.parse(data); },null]);
	services.push(["/POSTableService",{func : "getAllZone",posId:SharedData.posId},function(data,args) { SharedData.Zone = JSON.parse(data); },null]);
	arrayservice(services,LoadSharedServiceSuccess,null);
}
function LoadSharedServiceSuccess()
{ 
	DisplayBlock();
	
	DelayLoad();
}

function DelayLoad(){
	var services = [];
	services.push(["/POSItemService",{func : "loadPOSTax"},function(data,args) { SharedData.POSTax = JSON.parse(data); },null]);
	services.push(["/POSItemService",{func : "loadPOSUnit"},function(data,args) { SharedData.POSUnit = JSON.parse(data); },null]);
	services.push(["/POSItemService",{func : "loadCategory", posId: SharedData.posId},function(data,args) { SharedData.Category = JSON.parse(data); },null]);
	services.push(["/POSItemService",{func : "loadItem", posId: SharedData.posId},function(data,args) { SharedData.Item = JSON.parse(data); },null]);
	services.push(["/POSTableService",{func : "getAllPOS",posId:SharedData.posId},function(data,args) {
		var POSlist = JSON.parse(data);
		if(POSlist.id == null)	for(var i=0;i<POSlist.length;i++) if(POSlist[i].id==SharedData.posId) SharedData.POS = POSlist[i];
		else SharedData.POS = POSlist;
	},null]);
	services.push(["/reasonCancelService",{func:"loadReasonCancel"},function(data,args) { SharedData.CancelReason = JSON.parse(data); },null]);
	arrayservice(services,DelayLoadSuccess,null);
}

function DelayLoadSuccess(){
	fillPOSItemTax();
	getPOSItemStructure();
	if(SharedData.POS.listPOSTaxString!=null){
		SharedData.POS.taxList = [];
		var tax = SharedData.POS.listPOSTaxString.split(",");
		for(var _i=0;_i<tax.length;_i++) SharedData.POS.taxList.push(getObjectFromSharedData(SharedData.POSTax,tax[_i]));
	}
}

function fillPOSItemTax(){
	for(var i=0;i<SharedData.Item.length;i++)
	{
		var rs = calPriceAndTax(SharedData.Item[i]);
		SharedData.Item[i].totalTax = rs.totalTax;
	}
}

function getPOSItemStructure(){
	var data = SharedData.Category;
	POSCatStructure(data);
	pushPOSItem();
	pushPOSFavItem();
	function POSCatStructure(data){
		var etc = [];
		for(var _i=0;_i<data.length;_i++)
			if(data[_i].parentCategory == 0){
				var obj = { data: data[_i], type: "Cat", items: [] }
				POSItemStructure.cats.push(obj);
			}
			else {
				var tmp = getPOSCat(POSItemStructure.cats,data[_i].parentCategory);
				if(tmp != null)
					tmp.items.push({ data: data[_i], type: "Cat", items: [] });
				else etc.push(data[_i]);
			}
		if(etc.length>0) POSCatStructure(etc);
	}
	function getPOSCat(struct,catId){
		var rs = null;
		for(var _i=0;_i<struct.length;_i++)
			if(struct[_i].data.id == catId && struct[_i].type == "Cat")
				return struct[_i];
			else
				if(struct[_i].items.length>0){
					rs = getPOSCat(struct[_i].items,catId);
					if(rs!=null) return rs;
				}
		return rs;
	}
	function pushPOSItem(){
		for(var _i=0;_i<SharedData.Item.length;_i++)
		{
			var tmp = getPOSCat(POSItemStructure.cats,SharedData.Item[_i].category);
			if(tmp!=null)
				tmp.items.push({ data: SharedData.Item[_i], type: "Item", items: [] });
		}
	}
}
function pushPOSFavItem(){
	POSItemStructure.fav = [];
	for(var _i=0;_i<SharedData.Item.length;_i++)
		if(SharedData.Item[_i].favourite>0) {POSItemStructure.fav.push({ data:SharedData.Item[_i], type: "Item"});}
}

function calPriceAndTax(item){
	var includeTax = SharedData.General.incloudetax;
	if(item.tax!=null){
		var taxobj = getSummationPOSTaxPercAndFix(item);
		return customCalTax(item.price,taxobj.percent,taxobj.fix);
	}
	else return customCalTax(item.price,0,0);
}

function calPriceAndTaxWithQuantity(item){
	var includeTax = SharedData.General.incloudetax;
	var rs = {taxPerItem:0,pricePerItem:item.price};
	if(item.tax!=null && item.tax.length > 0){
		rs.taxPerItem = 0;
		for(var i=0;i<item.tax.length;i++) rs.taxPerItem += (item.tax[i].taxValue>0)? item.tax[i].taxValue:0;
	}
	if(includeTax){
		var realTax = getObjectFromSharedData(SharedData.Item,item.item).totalTax;
		rs.displayPriceTotal = (rs.pricePerItem+realTax)*item.amount - item.discount;
	}
	else
		rs.displayPriceTotal = (rs.pricePerItem+rs.taxPerItem)*item.amount - item.discount;
	return rs;
}

function getSummationPOSTaxPercAndFix(item){
	var tax = item.tax.split(",");
	var percent = 0;
	var fix = 0;
	for(var _i=0;_i<tax.length;_i++)
	{
		var t = getObjectFromSharedData(SharedData.POSTax,tax[_i]);
		if(t.isPercent)	percent += t.currencyValue;
		else fix += t.currencyValue;
	}
	return {fix:fix,percent:percent};
}

function checkSessionPOS(objArray){
	if(objArray.result==false){
		var buttons = [{ title:"OK" , callback: function(thisdialog,args){window.location.assign("/../logout.jsp");}}];
   		customDialog(Warning,ForceLogout,null,buttons,null);
   		return true;
	}
}

function getSharedDataPOSTax(){var e=0;var t=0;for(var n=0;n<SharedData.POSTax.length;n++)if(!SharedData.POSTax[n].status||!SharedData.POSTax[n].avaliable)continue;else{if(SharedData.POSTax[n].isPercent)e+=SharedData.POSTax[n].currencyValue;else t+=SharedData.POSTax[n].currencyValue}return{fix:t,perc:e}}